package com.example.musicplayer.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;


import com.example.musicplayer.R;
import com.example.musicplayer.entity.Music;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.util.MusicNotification;

import java.util.List;

public class PopUpMusicListAdapter extends RecyclerView.Adapter<PopUpMusicListAdapter.ViewHolder> {

    private List<Music> mMusics;
    private Context context;
    private MusicService.MusicBinder mMusicService = MusicService.getMusicService();
    private TextView cacheMusicName;
    private TextView cacheArtistName;
    private TextView cacheDot;

    public PopUpMusicListAdapter(Context context, List<Music> musics) {
        this.mMusics = musics;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pop_up_music_list_item, parent, false);
        return new ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final TextView musicName = holder.musicView.findViewById(R.id.pop_up_music_list_music_name);
        final TextView artistName = holder.musicView.findViewById(R.id.pop_up_music_list_artist_name);
        final TextView dot = holder.musicView.findViewById(R.id.pop_up_list_dot);
        final Music music = mMusics.get(position);
        Music currentMusic = mMusicService.getCurrentMusic();
        if (music.getId().equals(currentMusic.getId())) {
            setSelectedStyle(musicName);
            setSelectedStyle(artistName);
            setSelectedStyle(dot);
            cacheMusicName = musicName;
            cacheArtistName = artistName;
            cacheDot = dot;
        }

        holder.musicName.setText(music.getName());
        holder.artistName.setText(music.getArtist());
        holder.clickArea.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                int index = mMusics.indexOf(music);
                mMusicService.setCurrentMusicList(mMusics, index);
                Intent intent = new Intent();
                intent.setAction(MusicNotification.ACTION_LIST);
                context.sendBroadcast(intent);
                // 设计样式
                if (cacheMusicName != null) {
                    setUnSelectedStyle(cacheMusicName);
                    setUnSelectedStyle(cacheArtistName);
                    setUnSelectedStyle(cacheDot);
                }
                setSelectedStyle(musicName);
                setSelectedStyle(artistName);
                setSelectedStyle(dot);
                cacheMusicName = musicName;
                cacheArtistName = artistName;
                cacheDot = dot;
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setSelectedStyle(TextView textView) {
        Log.i("==================", "设置了选中样式");
        textView.setTextColor(context.getResources().getColorStateList(R.color.colorPrimary));
        textView.setSelected(true);
        textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
        textView.setMarqueeRepeatLimit(-1);
        textView.setSingleLine(true);
    }

    private void setUnSelectedStyle(TextView textView) {
        Log.i("==================", "设置了未选中样式");
        textView.setTextColor(Color.GRAY);
        textView.setSelected(false);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setFocusable(false);
        textView.setFocusableInTouchMode(false);
        textView.setMarqueeRepeatLimit(-1);
        textView.setSingleLine(true);
    }

    @Override
    public int getItemCount() {
        return mMusics.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView musicName; // 音乐名称
        TextView artistName;// 艺术家名称
        ImageView clearMusic; //清理按键
        LinearLayout clickArea;// 列表点击区域
        View musicView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicName = itemView.findViewById(R.id.pop_up_music_list_music_name);
            artistName = itemView.findViewById(R.id.pop_up_music_list_artist_name);
            clearMusic = itemView.findViewById(R.id.clear_pop_up_music_list);
            musicView = itemView;
            clickArea = itemView.findViewById(R.id.pop_click);
        }
    }

}
